import {AbstractEntity} from "../basic/AbstractEntity";
import {Position} from "../basic/Position";
import {Orientation} from "../basic/Orientation";
import {IEntity} from "../basic/IEntity";
import {Tank} from "./Tank";
import {Drawer} from "../../core/Drawer";
import {Constants} from "../../core/Constants";
import {CoordinatesUtil} from "../../utils/CoordinatesUtil";
import {Game} from "../../core/Game";
import {NumberUtil} from "../../utils/NumberUtil";

export class Bullet extends AbstractEntity {
  ownerPlayerId: number;
  speedX: number;
  speedY: number;

  destroy: boolean = false;

  damage: number = 10;

  static drawSize: number = 10;

  static readonly baseSpeedPerSecond: number = NumberUtil.resolved(Constants.tankDefaultMaxSpeedPerSecond);

  static imageTemp:HTMLImageElement|undefined//创建这个对象的开销很大 因此进行缓存。
  image: HTMLImageElement;//创建这个对象的开销很大 因此进行缓存。

  game:Game

  constructor(logicPosition: Position, logicOrientation: Orientation, ownerPlayerId: number, speedX: number, speedY: number, game: Game) {
    super(logicPosition, logicOrientation);
    this.game = game
    this.ownerPlayerId = ownerPlayerId;
    this.speedX = speedX;
    this.speedY = speedY;
    if(Bullet.imageTemp==undefined){
      this.image = new Image();
      this.image.src = "assets/img/bullet.png";
      Bullet.imageTemp = this.image
    }else{
      this.image = Bullet.imageTemp!;
    }

    // this.image.src = "https://i04piccdn.sogoucdn.com/9ec099543addf06f";



  }

  shouldRemove(): boolean {
    return this.destroy;
  }

  override onCollide(other: IEntity): void {
    super.onCollide(other)
    if (other instanceof Tank) {
      this.destroy = true;
    }
  }

  getSize(): number {
    return Bullet.drawSize;
  }

  draw(ctx: CanvasRenderingContext2D): void {


    Drawer.defaultEntityDraw(ctx, this.image, this.drawPosition, this.drawOrientation, Bullet.drawSize);


  }



  override update() {
    super.update();
    this.move()
  }
  move() {
    if (this.speedX === 0 && this.speedY === 0) {

      return;
    }

    this.logicPosition = CoordinatesUtil.moveBySpeed(this.logicPosition, this.speedX , this.speedY );
  }
}
